package org.mspring.mlog.service;

import java.util.Date;

import org.mspring.mlog.dao.JobLogDao;
import org.mspring.mlog.entity.JobLog;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class JobLogService {

    @Autowired
    private JobLogDao jobLogDao;

    /**
     * @param jobId
     * @return
     */
    public JobLog findLastestExecute(long jobId) {
        return jobLogDao.findLastestExecute(jobId);
    }

    public Page<JobLog> listPage(QueryBuilder queryBuilder, Page<JobLog> page) {
        return jobLogDao.listPage(queryBuilder, page);
    }

    /**
     * @param days
     */

    public void removeJobLog(int days) {
        if (days > 0) {
            Date date = DateUtils.addDays(new Date(), -days);
            String dateStr = DateUtils.format(date, DateUtils.YYYY_MM_DD_HH_MM_SS);
            jobLogDao.clearBeforeTime(dateStr);
        } else {
            jobLogDao.clear();
        }
    }

    /**
     * 创建任务日志
     *
     * @param jobLog
     * @return
     */

    public Long insert(JobLog jobLog) {
        return jobLogDao.insert(jobLog);
    }
}
